<?php
use OSS\OssClient;
use OSS\Core\OssException;

class OSS {
    private $accesskey_id = '';
    private $accesskey_secret = '';
    private $host = '';
    private $_service;
    const DOMAIN = 'http://image.hanlingsha.love';
    const IMAGE_BUCKET = 'zhihuishu-image';

    public function __construct(){
        $this->accesskey_id = $_SERVER['OSS_ACCESS_ID'];
        $this->accesskey_secret = $_SERVER['OSS_ACCESS_SECRET'];
        $this->host = $_SERVER['OSS_HOST'];
        $this->_service = new OssClient($this->accesskey_id, $this->accesskey_secret, $this->host); 
    }
    
    public function upload($bucket, $file, $name) {
        try {
            $ret = $this->_service->uploadFile($bucket, $name, $file);
            if (false === $ret) {
                return false;
            }
            $url = self::DOMAIN.$name;
            return $url;
        } catch (Exception $e) {
            log_message('error', 'oss exception: msg['.$e->getMessage().']'); 
            return false;
        }
    }

    public function upload_img($bucket, $file, $name) {
        $url = $this->upload($bucket, $file, $name); 
        if (!$url) {
            return false; 
        }
        $ch = curl_init();
        $tmp= substr(self::DOMAIN, -1);
        if($tmp === '/'){
            $url = self::DOMAIN.$name; 
        }else{
            $url = self::DOMAIN.'/'.$name;
        }
        curl_setopt($ch, CURLOPT_URL, $url.'?x-oss-process=image/info');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        if (false === $output) {
            log_message('error', 'oss: get img info error, url['.$url.']'); 
            return false;
        }
        return array('url'=>$url, 'info'=>json_decode($output, true));
    }

    /**
     * 
     *
     * @param [type] $file file in tmp $_FILE['']['tmp_name']
     * @param [type] $name file real name
     * @param string $dir
     * @return void
     */
    public function upload_pic($file, $name, $dir='novel/cover') {
        $SMALL_LIMIT =  125;
        $ret = $this->upload_img(self::IMAGE_BUCKET, 
                                 $file,
                                 $this->_get_upload_name($dir, $name));
        if (false === $ret) {
            return $ret;  
        }
        $result = array();
        $height = $ret['info']['ImageHeight']['value'];
        $width = $ret['info']['ImageWidth']['value'];
        $normal = array('url' => $ret['url'], 'w' => $width, 'h' => $height);
        $result['n'] = $normal;
        if ($height > $SMALL_LIMIT) {
            $result['s'] = array(
                'url' => $ret['url'].'@'.$SMALL_LIMIT.'h',
                'h' => $SMALL_LIMIT, 
                'w' => intval($SMALL_LIMIT * $width / $height)
            ); 
        } else {
            $result['s'] = $normal;
        }
        return $result;
    }

    private function _get_upload_name($type, $name) {
        mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
        $uuid = strtoupper(md5(uniqid(rand(), true)));
        return $type.'/'.date("Y-m-d").'/'.$uuid.'.'.pathinfo($name, PATHINFO_EXTENSION);
    }
}